package com.wolfpire.workflow.utils;

import java.util.List;

import com.wolfpire.workflow.em.Conclusion;
import com.wolfpire.workflow.om.MyTask;
import com.wolfpire.workflow.om.Node;

/**
 * 计算会签结果
 * @author liushx
 *
 */
public class CountersignCalculator {
	
	/**
	 * 会签决策（一票否决制）
	 * @param signList
	 * @param isAllSign
	 * @param node
	 * @return
	 */
	public Conclusion signAll(List<MyTask> signList, boolean isAllSign,String signType) {
		
		if("2".equals(signType)){
			return Conclusion.DENY;
		}
		if (isAllSign) {// 所有票投完了
			return Conclusion.AGREE;
		}else{
			if("1".equals(signType)){
				return Conclusion.CONTINUE;
			}
		}
		for (MyTask myTask : signList) {
			if (Conclusion.DENY.getIndex().equals(myTask.getOperateTypeIndex())) {
				// 一旦出现否决票，立刻作为否决处理
				return Conclusion.DENY;
			}
		}
		return Conclusion.CONTINUE;
	}
	
	public Conclusion signAll(List<MyTask> signList, boolean isAllSign) {
		for (MyTask myTask : signList) {
			if (Conclusion.DENY.getIndex().equals(myTask.getOperateTypeIndex())) {
				// 一旦出现否决票，立刻作为否决处理
				return Conclusion.DENY;
			}
		}

		if (isAllSign) {// 所有票投完了
			return Conclusion.AGREE;
		}
		
		return Conclusion.CONTINUE;
	}
	
	/**
	 *  一票通过制
	 * @param signList
	 * @param isAllSign
	 * @return
	 */
	public Conclusion signOne(List<MyTask> signList, boolean isAllSign) {
		
		for (MyTask myTask : signList) {
			if (Conclusion.AGREE.getIndex().equals(myTask.getOperateTypeIndex())) {
				// 一旦出现通过，立刻作为通过处理
				return Conclusion.AGREE;
			}
		}

		if (isAllSign) {// 所有票投完了
			return Conclusion.AGREE;
		}
		
		return Conclusion.CONTINUE;
	}
	
	/**
	 * 意见收集制
	 * @param signList
	 * @param isAllSign
	 * @return
	 */
	public Conclusion feedback(boolean isAllSign) {
		//所有票投完了
		if (isAllSign) {
			return Conclusion.AGREE;
		}
		
		return Conclusion.CONTINUE;
	} 
	
	/**
	 * 按比例通过
	 * @param signList
	 * @param isAllSign
	 * @param node
	 * @return
	 */
	public Conclusion percent(List<MyTask> signList, boolean isAllSign, Node node) {
		int agreeSize = 0;
		for (MyTask myTask : signList) {
			if (Conclusion.AGREE.getIndex().equals(myTask.getOperateTypeIndex())) {
				agreeSize++;
				// 一旦出现通过，立刻作为通过处理
				//return Conclusion.AGREE;
			}
		}
		/*System.out.println("同意人数为 [{}] 最少同意人数[{}] 总意见数[{}]=="+ new Object[] 
		 * { agreeSize, node.getStrategyValue(), signList.size() });
		 */
		if (isAllSign) {
			return (agreeSize >= node.getStrategyValue()) ? Conclusion.AGREE : Conclusion.DENY;
		} else{
			return (agreeSize >= node.getStrategyValue()) ? Conclusion.AGREE : Conclusion.CONTINUE;
		}

	}

}
